package org.atlantis.evnlib;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

import org.atlantis.evnlib.execution.EventExecutor;

/**
 * 事件对象。
 * 
 * @author <a href="mailto:seenoevil.cn@gmail.com">李曦</a><br/>
 *         Created at: 2011-6-30 下午09:14:44
 */
class Event {

	private EventDef eventDef;
	private Set<EventReceiver> receivers = new CopyOnWriteArraySet<EventReceiver>();
	private EventExecutor evnExec;

	Event(EventDef eventDef, EventExecutor evnExec) {
		this.eventDef = eventDef;
		this.evnExec = evnExec;
	}

	void addReceivers(Collection<EventReceiver> eventReceivers) {
		receivers.addAll(eventReceivers);
	}

	boolean removeReceivers(Collection<EventReceiver> eventReceivers) {
		return receivers.removeAll(eventReceivers);
	}

	void fire(EventArg eventArg) {
		for (EventReceiver eventReceiver : receivers)
			evnExec.execute(eventReceiver, eventArg);
	}

	EventDef getEventDef() {
		return eventDef;
	}

	Set<EventReceiver> getEventReceivers() {
		return receivers;
	}

	@Override
	public String toString() {
		return String.format("Event<name:%s, receiverSize:%d>", eventDef.getEventName(), receivers.size());
	}
}